package cn.wolfcode.wms.controller;

import cn.wolfcode.wms.domain.Brand;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.IBrandService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private IBrandService brandService;

    @RequestMapping("/list")
    @RequiredPermission("品牌列表")
    public String list(QueryObject qo, Map map) {
        map.put("pageResult", brandService.query(qo));
        return "brand/list";
    }

    @RequestMapping("/input")
    @RequiredPermission("品牌编辑")
    public String input(Long id, Map map) {
        if (id != null) {
            map.put("brand", brandService.getById(id));
        }
        return "brand/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission("更新或新增品牌")
    public String saveOrUpdate(Brand brand) {
        try {
            brandService.saveOrUpdate(brand);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/brand/list.do";
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiredPermission("删除品牌")
    public JSONResult delete(Long id) {
        try {
            brandService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            // 出错时返回错误信息
            return JSONResult.mark(e.getMessage());
        }
        return JSONResult.success("删除成功！");
    }
}
